package ua.com.minus1.lookbattle;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import ua.com.minus1.lookbattle.net.HttpManager;
import ua.com.quietlycoding.android.picker.NumberPicker;

/**
 * Created with IntelliJ IDEA.
 * User: Flash
 * Date: 22.08.12
 * Time: 11:59
 * To change this template use File | Settings | File Templates.
 */
public class ConvertDialog extends Dialog implements View.OnClickListener, NumberPicker.OnChangedListener {
    private NumberPicker picker;
    private TextView clicks;
    private Params params;
    private Button convertBtn;
    private Button cancelBtn;
    private HttpManager httpManager;
    private OnConvertListener onConvertListener;

    private final int CONVERT_FACTOR = 1;

    public ConvertDialog(Context context) {
        super(context);
        setContentView(R.layout.fragment_dialog_convert);

        httpManager = new HttpManager(context);
    }

    public ConvertDialog setConvertListener(OnConvertListener onConvertListener) {
        this.onConvertListener = onConvertListener;
        return this;
    }

    public void onClick(View view) {
        if (view == convertBtn) {
            convert();
        } else if (view == cancelBtn) {
            cancel();
        }
    }

    public void setParams(Bundle b) {
        params = parseBundle(b);
        setupViews();
    }

    private void setupViews() {
        picker = (NumberPicker) findViewById(R.id.convertNumberPicker);
        clicks = (TextView) findViewById(R.id.convertClicks);
        convertBtn = (Button) findViewById(R.id.convertDialogConvertBtn);
        convertBtn.setOnClickListener(this);
        cancelBtn = (Button) findViewById(R.id.convertDialogCancelBtn);
        cancelBtn.setOnClickListener(this);


        picker.setRange(0, (int) Math.floor(params.clicks * CONVERT_FACTOR));
        picker.setCurrent(params.bonuses);
        picker.setOnChangeListener(this);
        updateClicks(params.clicks);
    }

    private void updateClicks(int clicksValue) {
        clicks.setText(String.valueOf(clicksValue));
    }

    public void convert() {
        int bonuses = picker.getCurrent() - params.bonuses;
        onConvertListener.onClick(this, params.position, params.lookId, bonuses);
    }

    public static final Bundle getBundle(int position, int lookId, int bonuses, int clicksNum) {
        Bundle b = new Bundle();
        b.putInt("position", position);
        b.putInt("lookId", lookId);
        b.putInt("bonuses", bonuses);
        b.putInt("clicks", clicksNum);
        return b;
    }

    private static final Params parseBundle(Bundle b) {
        Params p = new Params();
        p.position = b.getInt("position");
        p.lookId = b.getInt("lookId");
        p.bonuses = b.getInt("bonuses");
        p.clicks = b.getInt("clicks");
        return p;
    }

    public void onChanged(NumberPicker picker, int oldVal, int newVal) {
       updateClicks((int) params.clicks - (newVal - params.bonuses) / CONVERT_FACTOR);
    }

    private static class Params{
        public int bonuses;
        public int clicks;
        public int lookId;
        public int position;
    }

    public interface OnConvertListener{
        void onClick(DialogInterface dialogInterface, int viewPosition, int lookId, int bonuses);
    }

}
